import {
	cancelOrderApi,
	changeOrderApi,
	reserveOrderApi
} from '@/api/order'
import {
	addReserveApi,
	wxPayApi
} from '@/api/reserve';
import {
	useUserStore
} from '@/store/user';

const userStore = useUserStore();
import {
	defineStore
} from 'pinia'

export const useOrderStore = defineStore('order', {
	state: () => ({
		// 步骤1：体检人信息 (from select page)
		customerInfo: {
			id: -1,
			planId: -1,
			cusotmerId: -1,
			name: '',
			phone: '',
			brandid:"",
			brand_list:[],
			ismarried:"",
			license_type: 0,
			license_number: '',
			gender: 0,
			address_area: '',
			address_detail: '',
			packageid:""
		},

		// 选择的品牌
		selectedBrand: null,

		// 选择的套餐
		selectedPackage: null,

		// 选择的加项
		selectedAddons: [],
		// 选择的机构
		selectedOrganization: {
			name: '',
			address: '',
			storeId: -1,
			examDate: null,
			examTime: null
		},

		// 支付信息
		paymentInfo: {
			totalAmount: 0,
			discountAmount: 0,
			finalAmount: 0,
		},

		// 订单信息
		order: {
			id: -1,
			reserveNo: null,
			status: 1,
			paystatus: 0,
			bookingno:null
		}
	}),

	getters: {
		// 计算总金额
		totalAmount: (state) => {
			let total = 0
			if (state.selectedPackage) {
				total += state.selectedPackage.price || 0
			}
			if (state.selectedAddons.length > 0) {
				total += state.selectedAddons.reduce((sum, addon) => sum + (addon.price || 0), 0)
			}
			return total
		},
	},

	actions: {
		// 设置体检人信息
		setCustomerInfo(info) {
			console.log(info)
			this.customerInfo = {
				...this.customerInfo,
				...info
			}
		},

		// 设置选择的品牌
		setBrand(brand) {
			this.selectedBrand = brand
		},

		// 设置选择的套餐
		setPackage(packageInfo) {
			console.log(packageInfo,"packageInfo")
			if(packageInfo?.packageid){
				packageInfo['packageid_copy']=packageInfo?.packageid
			}
			this.selectedPackage = packageInfo
			
		},

		// 添加加项
		addAddon(addon) {
			const existingIndex = this.selectedAddons.findIndex(item => item.id === addon.id)
			if (existingIndex === -1) {
				this.selectedAddons.push(addon)
			}
		},

		// 移除加项
		removeAddon(addonId) {
			const index = this.selectedAddons.findIndex(item => item.id === addonId)
			if (index > -1) {
				this.selectedAddons.splice(index, 1)
			}
		},

		// 设置选择的机构
		setOrganization(org) {
			this.selectedOrganization = org
		},

		// 更新支付信息
		updatePaymentInfo(paymentInfo) {
			this.paymentInfo = {
				...this.paymentInfo,
				...paymentInfo
			}
			this.paymentInfo.totalAmount = this.totalAmount
			this.paymentInfo.finalAmount = this.paymentInfo.totalAmount - this.paymentInfo.discountAmount
		},

		// 设置订单信息
		setOrder(data) {
			this.order = data;
		},

		// 重置订单数据
		resetOrder() {
			this.customerInfo = {
				id: -1,
				planId: -1,
				cusotmerId: -1,
				name: '',
				phone: '',
				license_type: 0,
				license_number: '',
				gender: 0,
				address_area: '',
				address_detail: '',
				brandid:"",
				brand_list:[],
				packageid:""
			};
			this.selectedBrand = null;
			this.selectedPackage = null;
			this.selectedAddons = [];
			this.selectedOrganization = {
				name: '',
				address: '',
				storeId: -1,
				examDate: null,
				examTime: null
			};
			this.paymentInfo = {
				totalAmount: 0,
				discountAmount: 0,
				finalAmount: 0,
			};
			this.order = {
				id: -1,
				reserveNo: null,
				status: 1,
				paystatus: 0,
				bookingno:null
			};
		},

		// 生成订单 暂时不用了
		async createOrder() {
			// return new Promise((resolve, reject) => {
			// 	try {
			// 		reserveOrderApi({
			// 			id: this.customerInfo.id,
			// 			storeName: this.selectedOrganization.name,
			// 			storeId: this.selectedOrganization.storeId,
			// 			additionsId: this.selectedAddons.map(item => {
			// 				return item.additionsid
			// 			}),
			// 			// packageId: this.selectedPackage.packageid,
			// 			packageId: "2347735101",
			// 			examDate: this.selectedOrganization.examDate,
			// 			examTime: this.selectedOrganization.examTime
			// 		}).then(res => {
			// 			this.order.paystatus = 0;
			// 			// setTimeout(res=>{
			// 			addReserveApi({
			// 				id: this.customerInfo.id,
			// 				uid: userStore.userInfo.id,
			// 				// price: this.paymentInfo.finalAmount,
			// 				examDate: this.selectedOrganization.examDate,
			// 				examTime: this.selectedOrganization.examTime
			// 			}).then(() => { 
			// 				resolve()
			// 			}).catch((err)=>{
			// 				reject(err)
			// 			})
			// 			// },3000)
			// 		}).catch((err)=>{
			// 			reject(err)
			// 		})
			// 	} catch (error) {
			// 		reject(error)
			// 		throw error
			// 	}
			// })
		},

		async changeOrder() {
			return changeOrderApi({
				id: this.order.id,
				reserveNo: this.order.reserveNo,
				examDate: this.selectedOrganization.examDate,
				packageId: this.selectedPackage.packageid,
				storeId: this.selectedOrganization.storeId,
				storeName:this.selectedOrganization.name,
				examTime:this.selectedOrganization.examTime
			});
		},

		async cancelOrder({id,reserveNo}) {
			return cancelOrderApi({
				reserveNo,
				id
			});
		},
	},

	persist: {
		enabled: false,
	}
})